package com.wy.authority.controller;

import com.wy.authority.entity.Menu;
import com.wy.authority.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author 忘忧
 * @Date Created in 2021/1/13 9:45
 * @Description:
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping("findall")
    public List<Map<String,Object>> tree(){
        //查询出所有的一级菜单 p_id=0
        List<Menu> menus = menuService.find();
        //以list集合方式存储map值
        List<Map<String ,Object>> list =new ArrayList<>();
        if (menus.size()>0){
            for (Menu menu:menus){
                Map<String,Object> map = new LinkedHashMap<>();
                map.put("id",menu.getMenu_id());
                map.put("menu_id",menu.getMenu_id());
                map.put("parent_id",menu.getParent_id());
                map.put("menu_name",menu.getMenu_name());
                map.put("order_num",menu.getOrder_num());
                map.put("path",menu.getPath());
                map.put("icon",menu.getIcon());
                map.put("create_time",menu.getCreate_time());
                map.put("perms",menu.getPerms());
                map.put("component",menu.getComponent());
                map.put("menu_type",menu.getMenu_type());
                map.put("visible",menu.getVisible());
                map.put("status",menu.getStatus());
                map.put("is_cache",menu.getIs_cache());
                map.put("children",getChildren(menu.getMenu_id()));
                list.add(map);
            }
        }
        return list;
    }

    /**
     * 递归
     *
     * @param menu_id
     * @return
     */
    public List<Object> getChildren(Integer menu_id){
        List<Object> list =new ArrayList<>();
        List<Menu> menus =menuService.find2(menu_id);
        for(Menu menu:menus){
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("id",menu.getMenu_id());
            map.put("menu_id",menu.getMenu_id());
            map.put("parent_id",menu.getParent_id());
            map.put("menu_name",menu.getMenu_name());
            map.put("order_num",menu.getOrder_num());
            map.put("icon",menu.getIcon());
            map.put("path",menu.getPath());
            map.put("create_time",menu.getCreate_time());
            map.put("perms",menu.getPerms());
            map.put("component",menu.getComponent());
            map.put("menu_type",menu.getMenu_type());
            map.put("visible",menu.getVisible());
            map.put("status",menu.getStatus());
            map.put("is_cache",menu.getIs_cache());
            map.put("children",getChildren(menu.getMenu_id()));
            list.add(map);
        }
        return list;
    }

    //查询菜单
    @GetMapping("/findmenu")
    public List<Menu> findall(){
        List<Menu> list =menuService.find_menu();
        return list;
    }

    //插入菜单
    @PostMapping("/insert")
    public String insert(@RequestBody Menu menu) {
        menu.setCreate_time(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        System.out.println(menu);
        menuService.insert(menu);
        return "success";
    }

    //删除
    @DeleteMapping("/delete/{menu_id}")
    public void delete(@PathVariable("menu_id") Integer menu_id){
        menuService.delete(menu_id);
    }

    //通过id查询
    @GetMapping("/findbyid/{menu_id}")
    public List<Menu> find_byid(@PathVariable("menu_id") Integer menu_id){
        List<Menu> list= menuService.findbyid(menu_id);
        System.out.println(list);
        return list;
    }

    //更新
    @PutMapping("/update")
    public String update(@RequestBody Menu menu){
        menuService.update(menu);
        return "success";
    }
}



